package com.rrd.dw.mr.gzip.kafka;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import com.rrd.dw.utils.DateUtil;
import com.rrd.dw.utils.MD5Util;
import com.ucredit.babel.common.model.Job;
import com.ucredit.babel.common.model.alibaba2.AliPayOrder;
import com.ucredit.babel.common.model.alibaba2.AliTaobaoInfo;
import com.ucredit.babel.common.model.alibaba2.AliTaobaoOrder;
import com.ucredit.babel.common.model.alibaba2.AliTaobaoOrderProduct;
import com.ucredit.babel.common.model.alibaba2.AliTaobaoPayInfo;
import com.ucredit.babel.common.model.alibaba2.AliTaobaoRecord;
import com.ucredit.babel.common.model.alibaba2.MessageType;
import com.ucredit.babel.common.model.taobao.TaobaoItem;
/**
 * 解析阿里淘宝交易行信息
 * @author liubaoxin
 *
 */
@Deprecated
public class AliTaoBaoParse {
	public static final String CRT001 =  "\001";
	public static final Pattern CRT001_PATTERN =  Pattern.compile("\001");
	private  Text val = new Text();
	private  Text key= new Text();
	private  String user_id="";
	private String timestamstr="";
	private String job_id ="";
	private String account_name ="";
	private StringBuilder sb = new StringBuilder(500);
	/**
	 * 解析阿里支付基本信息，job信息
	 * @param call
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	public  void parseAliTaobaoBase(Context context,AliTaobaoRecord taobaoRecord,String successful,String dataJson) throws IOException, InterruptedException{
		
		StringBuilder sb = new StringBuilder(200);
    	Job job = taobaoRecord.getJob();
    	AliTaobaoInfo taobaoInfo= taobaoRecord.getTaobao();
    	AliTaobaoPayInfo payInfo  = taobaoRecord.getAliTaobaoPayInfo();
    	MessageType messageType = taobaoRecord.getMessType();
  
    	if(taobaoInfo==null ){
    		taobaoInfo = new AliTaobaoInfo();
    	}
    	if(payInfo==null ){
    		payInfo = new AliTaobaoPayInfo();
    	}
    	if(messageType==null){
    		messageType = new MessageType();
    	}
    	 this.timestamstr=taobaoRecord.getTimestamp()==null?"":DateUtil.formateDateStr(new Date(taobaoRecord.getTimestamp()));
    	 this.job_id = job.getJobID()==null?"":job.getJobID();
    	 this.user_id = MD5Util.getMd5(job_id + (timestamstr == null ? "" : timestamstr));
    	
    	sb.delete(0,sb.length());
    	sb.append(user_id).append(CRT001).append(job_id).append(CRT001).append(timestamstr).append(CRT001);
    	//昵称
    	sb.append(taobaoInfo.getNickName()==null?"":taobaoInfo.getNickName()).append(CRT001);
    	//真实姓名（有*）
    	sb.append(taobaoInfo.getRealName()==null?"":taobaoInfo.getRealName()).append(CRT001);
    	//生日
    	sb.append(taobaoInfo.getBirthday()==null?"":taobaoInfo.getBirthday()).append(CRT001);
    	//性别
    	sb.append(taobaoInfo.getSex()==null?"":taobaoInfo.getSex()).append(CRT001);
    	//登录名
    	sb.append(taobaoInfo.getLoginName()==null?"":taobaoInfo.getLoginName()).append(CRT001);
    	//来源用户手机号
    	sb.append(taobaoInfo.getSourceUserMobile()==null?"":taobaoInfo.getSourceUserMobile()).append(CRT001);
    	//来源用户身份证号
    	sb.append(taobaoInfo.getSourceUserIdno()==null?"":taobaoInfo.getSourceUserIdno()).append(CRT001);
    	//来源
    	sb.append(taobaoInfo.getSource()==null?"":taobaoInfo.getSource()).append(CRT001);
    	
    	//消息通知类型
    	sb.append(messageType.getType()==null?"":messageType.getType()).append(CRT001);
    	//支付宝红包
    	sb.append(payInfo.getRedPack()==null?"":payInfo.getRedPack()).append(CRT001);
    	//支付宝余额
    	sb.append(payInfo.getZhifubaoAmount()==null?"":payInfo.getZhifubaoAmount()).append(CRT001);
    	//银行卡数
    	sb.append(payInfo.getBankCradCount()==null?"":payInfo.getBankCradCount()).append(CRT001);
    	
    	//蚂蚁花呗的授信额度
    	sb.append(payInfo.getHuabeiLimit()==null?"":payInfo.getHuabeiLimit()).append(CRT001);
    	//集分宝
    	sb.append(payInfo.getJifenbao()==null?"":payInfo.getJifenbao()).append(CRT001);
    	
    	//是否结束戳
    	sb.append(job.isFinished()==null?"":job.isFinished()).append(CRT001);
    	//是否请求
    	sb.append(job.isRequested()==null?"":job.isRequested()).append(CRT001);
    	//是否成功
    	sb.append(successful).append(CRT001);
    	//起始时间
    	sb.append(job.getStartTime()==null?"":DateUtil.formateDateStr(job.getStartTime())).append(CRT001);
    	//结束时间
    	sb.append(job.getEndTime()==null?"":DateUtil.formateDateStr(job.getEndTime())).append(CRT001);
    	//系统ID
    	sb.append(job.getSystemID()).append(CRT001);
    	//查询编号
    	sb.append(job.getQuery()).append(CRT001);
    	//尝试次数
    	sb.append(job.getTryTimes()==null?"0":job.getTryTimes()).append(CRT001);
    	//JOB类型
    	sb.append("").append(CRT001);
    	//结果信息
    	sb.append("").append(CRT001);
    	//结果码
    	sb.append("");
    	
    	key.set("aliTaobaoBase");
    	val.set(sb.toString().replace("\n", " "));
    	context.write(key, val);
    	sb.delete(0,sb.length());
    	sb.append(job_id).append(CRT001).append(dataJson).append(CRT001).append(job.getSystemID());
    	key.set("aliTaobao_datajson");
    	val.set(sb.toString().replace("\n", " "));
    	context.write(key, val);
    	
    	 
	}
	
 
	/**
	 * 淘宝订单数据
	 * @param taobaoRecord
	 * @throws InterruptedException 
	 * @throws IOException 
	 */
	public   void parseAliTaobaoOrder(Context context, AliTaobaoRecord taobaoRecord) throws IOException, InterruptedException{
		AliTaobaoInfo taobaoInfo= taobaoRecord.getTaobao();
		if(taobaoInfo==null ){
			return;
		}
	
		List<AliTaobaoOrder> list=taobaoInfo.getOrders();
		if(list==null || list.size()<=0){
			return;
		}
    	StringBuilder sb = new StringBuilder(200);
    	
    	String s="";
    	
    	for(AliTaobaoOrder o :list){
    		  
    		sb.delete(0, sb.length());
    		//base id
    		sb.append(this.user_id).append(CRT001);
    		//数据平台job_id
    		sb.append(this.job_id).append(CRT001);
    		//时间戮
    		sb.append(timestamstr).append(CRT001);
    	
    		//交易金额
    		sb.append(o.getAmount()==null?"":o.getAmount()).append(CRT001);
    		//交易时间
    		sb.append(o.getTime()==null?"":DateUtil.formateDateStr(o.getTime())).append(CRT001);
    		//订单号
    		sb.append(o.getOrderNo()==null?"":o.getOrderNo()).append(CRT001);
    		//商品数量
    		sb.append(o.getOrderQuantity()==null?"":o.getOrderQuantity()).append(CRT001);
    		//运费
    		sb.append(o.getFreight()==null?"":o.getFreight()).append(CRT001);
    		//订单状态
    		sb.append(o.getOrderStatus()==null?"":o.getOrderStatus()).append(CRT001);
    		//收货地址
    		sb.append(o.getAddress()==null?"":o.getAddress());
    		s=sb.toString().replace("\n", " ");
    		s=MD5Util.getMd5(s)+CRT001+s;
    		key.set("aliTaobaoOrder");
        	val.set(s);
        	context.write(key, val);
        	parseAliTaobaoProduct(context,o);
    	}
    	
	}
	/**
	 * 淘宝订单商品数据
	 * @param context
	 * @param item
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public   void parseAliTaobaoProduct (Context context, AliTaobaoOrder o) throws IOException, InterruptedException{
		List<AliTaobaoOrderProduct> list = o.getProducts();
		if(list==null || list.size()<=0){
			return;
		}
		String s="";
		StringBuilder sb = new StringBuilder(200);
		String bstr="";
		sb.append(this.user_id).append(CRT001).append(job_id).append(CRT001).append(timestamstr).append(CRT001);
		//订单号
		sb.append(o.getOrderNo()==null?"":o.getOrderNo()); 
		bstr=sb.toString();
		
		for(AliTaobaoOrderProduct p :list){
			sb.delete(0, sb.length());
			sb.append(bstr).append(CRT001);
			//店名
			sb.append(p.getShopName()==null?"":p.getShopName()).append(CRT001);
			//商品原价 单位分
			sb.append(p.getOriginal()==null?"":p.getOriginal()).append(CRT001);
			//商品成交价 单位分
			sb.append(p.getPromotion()==null?"":p.getPromotion()).append(CRT001);
			//商品名称
			sb.append(p.getName()==null?"":p.getName()).append(CRT001);
			//购买的商品数量
			sb.append(p.getQuantity()==null?"":p.getQuantity()).append(CRT001);
			//商品属性
			sb.append(p.getAttr()==null?"":p.getAttr()).append(CRT001);
			//商品权益
			sb.append(p.getServiceInfo()==null?"":p.getServiceInfo());
			
			s=sb.toString().replace("\n", " ");
    		s=MD5Util.getMd5(s)+CRT001+s;
    		key.set("aliTaobaoProduct");
        	val.set(s);
        	context.write(key, val);
		}
	}
}
